<%
=begin
apps: node
platforms: kubernetes, tanzu-application-catalog
id: use_existing_database
title: Connect your application to an already existing database
category: configuration
weight: 30
=end %>

* Create a secret containing your database credentials (named *my-database-secret* as example), you can use the following options (set with *--from-literal*) to create the secret:

    host=YOUR_DATABASE_HOST
    port=YOUR_DATABASE_PORT
    username=YOUR_DATABASE_USER
    password=YOUR_DATABASE_PASSWORD
    database=YOUR_DATABASE_NAME
    YOUR_DATABASE_HOST, YOUR_DATABASE_PORT, YOUR_DATABASE_USER, YOUR_DATABASE_PASSWORD, and YOUR_DATABASE_NAME are placeholders that must be replaced with correct values.

* Deploy the Node chart specifying the secret name:

    mongodb.install=false
    externaldb.secretName=my-database-secret

### Provision a database using the Open Service Broker for Azure

* Install Service Catalog in your Kubernetes cluster following the steps described in the [Kubernetes official documentation](https://kubernetes.io/docs/tasks/service-catalog/install-service-catalog-using-helm/).
* Install the Open Service Broker for Azure in your Kubernetes cluster following [these instructions](https://github.com/Azure/open-service-broker-azure/tree/master/contrib/k8s/charts/open-service-broker-azure).

> TIP: you may want to install the osba chart setting the modules.minStability=EXPERIMENTAL to see all the available services.

       azure.subscriptionId=$AZURE_SUBSCRIPTION_ID
       azure.tenantId=$AZURE_TENANT_ID
       azure.clientId=$AZURE_CLIENT_ID
       azure.clientSecret=$AZURE_CLIENT_SECRET
       modules.minStability=EXPERIMENTAL

* Create and deploy a ServiceInstance to provision a database server in Azure cloud.

~~~
apiVersion: servicecatalog.k8s.io/v1beta1
kind: ServiceInstance
metadata:
  name: azure-mongodb-instance
  labels:
    app: mongodb
spec:
  clusterServiceClassExternalName: azure-cosmosdb-mongo-account
  clusterServicePlanExternalName: account
  parameters:
    location: YOUR_AZURE_LOCATION
    resourceGroup: mongodb-k8s-service-catalog
    ipFilters:
      allowedIPRanges:
      -  "0.0.0.0/0"
~~~

Please update the YOUR_AZURE_LOCATION placeholder in the above example.

* Deploy the Helm chart:

    mongodb.install=false
    externaldb.broker.serviceInstanceName=azure-mongodb-instance
    externaldb.ssl=true

Once the instance has been provisioned in Azure, a new secret should have been automatically created with the connection parameters for your application.

* Deploying the Helm chart enabling the Azure external database makes the following assumptions:

* You would want an Azure CosmosDB MongoDB database.
* Your application uses DATABASE_HOST, DATABASE_PORT, DATABASE_USER, DATABASE_PASSWORD, and DATABASE_NAME environment variables to connect to the database.

For more information on this, check out the [following documentation](https://github.com/kubernetes-bitnami/service-catalog).
